package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.take_delivery.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

//添加注解
@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
@SuppressWarnings("all") // 取消黄色警告线
public class PromotionAction extends BaseAction<Promotion> {

	@Action(value = "promotion_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {

		// 用webService技术，跨系统获取数据

		PageBean<Promotion> pageBean = WebClient
				.create("http://localhost:8080/bos_management/service/promotionService/pageQuery?page=" + page
						+ "&rows=" + rows)
				.accept(MediaType.APPLICATION_JSON).get(PageBean.class);

		// 将返回的结果存入值栈
		ServletActionContext.getContext().getValueStack().push(pageBean);

		return SUCCESS;
	}

	/**
	 * 获取促销详情的信息，静态页面化技术，第一次访问的时候会自己生成，后面存在后就不用再创建html文件了
	 * 
	 * @throws IOException
	 * @throws TemplateException
	 */
	@Action(value = "promotion_showDetails") 
	public String showDetials() throws IOException, TemplateException {
		// 先判断促销活动对应的html页面是否存在，如果存在直接返回
		String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemarker");

		File detailFile = new File(htmlRealPath + "/" + model.getId() + ".html");
		// 如果html文件不存在，查询数据库，结合freemaker模板生成页面
		if (!detailFile.exists()) {
			// freemaker
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			// 创建模板所在文件夹
			configuration.setDirectoryForTemplateLoading(
					new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/template")));
			// 获取模板对象
			Template template = configuration.getTemplate("promotion_detail.ftl");

			// 发送请求到数据库获取信息
			Promotion promotion = WebClient
					.create("http://localhost:8080/bos_management/service/promotionService/promotionDetials?id="
							+ model.getId())
					.accept(MediaType.APPLICATION_JSON).get(Promotion.class);

			Map<String, Object> parameterMap = new HashMap<String, Object>();
			// 要与模板获取的参数名保持一致
			parameterMap.put("promotion", promotion);

			// 合并输出，将内容和html文件
			template.process(parameterMap, new OutputStreamWriter(new FileOutputStream(detailFile), "utf-8"));

		}

		// 存在就直接返回
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");

		// 读写文件
		FileUtils.copyFile(detailFile, ServletActionContext.getResponse().getOutputStream());

		return NONE;
	}

}
